package org.ruixue.controller;

import cn.rkylin.oms.common.dto.DBLog;
import com.alibaba.fastjson.JSON;

import org.ruixue.request.ItemRequest;
import org.ruixue.response.BaseResponse;
import org.ruixue.service.ItemService;
import org.ruixue.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 商品同步
 * 
 * @author 崔棋 2020-04-09
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/item")
public class ItemController {

	@Autowired
	ItemService itemService;

	@Autowired
	LogUtil logUtil;
	
	/**
     * http://localhost:40005/microservice-yiyouyayin/item/downloadYiItemWhole
     * 全部商品下载 
     * 
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/downloadYiItemWhole", method = RequestMethod.GET)
    private ResponseEntity<BaseResponse> downloadYiItemWhole() {
        BaseResponse response = new BaseResponse();
        try {
            response = itemService.downloadYiItemWhole();
            logUtil.info(new DBLog("", "", "downloadYiItemWhole", JSON.toJSONString(response)));
        } catch (Exception e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSuccess("false");
            logUtil.error(new DBLog("", "", "downloadYiItemWhole", e.getMessage()), e);
        }
        return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
    }

	/**
     * http://localhost:40005/microservice-yiyouyayin/item/downloadYiItem?shopId=55ed507f052747a0aa97d56d63f53f99
     * 商品下载 按时间段 增量
     * 
	 * @param shopId
	 *            网店ID
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/downloadYiItem", method = RequestMethod.GET)
    private ResponseEntity<BaseResponse> downloadYiItem(@RequestParam String shopId) {
        BaseResponse response = new BaseResponse();
        try {
            response = itemService.downloadYiItem(shopId);
            logUtil.info(new DBLog(shopId, "", "downloadYiItem", JSON.toJSONString(response)));
        } catch (Exception e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSuccess("false");
            logUtil.error(new DBLog(shopId, "", "downloadYiItem", e.getMessage()), e);
        }
        return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
    }
	
	/**
	 * http://localhost:40005/microservice-yiyouyayin/item/downloadYiItemAll?shopId=55ed507f052747a0aa97d56d63f53f99
	 * 按店铺全部商品同步
	 * 
	 * @param shopId
	 *            网店ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/downloadYiItemAll", method = RequestMethod.GET)
	private ResponseEntity<BaseResponse> downloadYiItemAll(@RequestParam String shopId) {
		BaseResponse response = new BaseResponse();
		try {
			response = itemService.downloadYiItemAll(shopId);
			logUtil.info(new DBLog(shopId, "", "downloadYiItemAll", JSON.toJSONString(response)));
		} catch (Exception e) {
			e.printStackTrace();
			response.setMsg(e.getMessage());
			response.setSuccess("false");
			logUtil.error(new DBLog(shopId, "", "downloadYiItemAll", e.getMessage()), e);
		}
		return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
	}

	/**
	 * http://localhost:40005/microservice-yiyouyayin/item/downloadYiEcItemCode?ecItemCode=1b3f0fd9e1164c1d8eb9ad8e0f459aec
	 * 按店铺中平台商品编码
	 * 
	 * @param ecItemCode
	 *            平台商品编码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/downloadYiEcItemCode", method = RequestMethod.GET)
	private ResponseEntity<BaseResponse> downloadYiEcItemCode(@RequestParam String ecItemCode) {
		BaseResponse response = new BaseResponse();
		try {
			response = itemService.downloadYiEcItemCode(ecItemCode);
			logUtil.info(new DBLog(ecItemCode, "", "downloadYiEcItemCode", JSON.toJSONString(response)));
		} catch (Exception e) {
			e.printStackTrace();
			response.setMsg(e.getMessage());
			response.setSuccess("false");
			logUtil.error(new DBLog(ecItemCode, "", "downloadYiEcItemCode", e.getMessage()), e);
		}
		return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
	}
	
	/**
     * http://localhost:40005/microservice-yiyouyayin/item/listingYiItem?shopId=55ed507f052747a0aa97d56d63f53f99
     * 商品上架
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listingYiItem")
    private ResponseEntity<BaseResponse> listingYiItem(@RequestParam String shopId, @RequestBody ItemRequest itemRequest) {
        BaseResponse response = new BaseResponse();
        try {
            response = itemService.listingYiItem(shopId, itemRequest);
            logUtil.info(new DBLog(shopId, "", "listingYiItem", JSON.toJSONString(response)));
        } catch (Exception e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSuccess("false");
            logUtil.error(new DBLog(shopId, "", "listingYiItem", e.getMessage()), e);
        }
        return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
    }

    /**
     * http://localhost:40005/microservice-yiyouyayin/item/delistingYiItem?shopId=55ed507f052747a0aa97d56d63f53f99
     * 商品下架
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delistingYiItem")
    private ResponseEntity<BaseResponse> delistingYiItem(@RequestParam String shopId, @RequestBody ItemRequest itemRequest) {
        BaseResponse response = new BaseResponse();
        try {
            response = itemService.delistingYiItem(shopId, itemRequest);
            logUtil.info(new DBLog(shopId, "", "delistingYiItem", JSON.toJSONString(response)));
        } catch (Exception e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSuccess("false");
            logUtil.error(new DBLog(shopId, "", "delistingYiItem", e.getMessage()), e);
        }
        return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
    }
}
